package com.minyifang.wcspf.work.ui.home.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.home.view.HomePage3View;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/7/25.
 */
public class HomePage3Presenter extends BasePresenter<HomePage3View> {
    /**
     * 获取购物车数据
     */
    public void getCarData() {
        JSONObject jsonData = Constants.getAESJsonData(new HashMap<>(), UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCarData(requestBody), respond -> {
            getView().showCarData(respond);

        });
    }

    /**
     * 购物车商品数量添加减少
     *
     * @param mscId       id
     * @param mscQuantity 商品数量
     */
    public void addCarNum(String mscId, String mscQuantity) {
        Map<String, Object> map = new HashMap<>();
        map.put("mscId", mscId);
        map.put("mscQuantity", mscQuantity);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().addCarNum(requestBody), respond -> getView().showAddCarNumRes(respond));
    }

    public void delCarGoods(String comId) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId", comId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().delCarGoods(requestBody), respond -> getView().showDeleteRes(respond));
    }

    /**
     * 结算
     *
     * @param comId     购物车id
     * @param tipsMoney 满减包邮差价
     */
    public void calculateMoney(String comId, String tipsMoney) {
        Map<String, Object> map = new HashMap<>();
        map.put("mscId", comId);
        map.put("muchMon", tipsMoney);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().calculateMoney(requestBody), respond -> getView().showCalculateMoney(respond));
    }
}
